/**
 * 部门机构控制器
 * @author EchoHu
 * contract:elyzium_wow@hotmail.com
 */
package com.pms.privilege.dept.controller;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pms.privilege.dept.model.DeptModel;
import com.pms.privilege.dept.service.DeptService;
import com.pms.privilege.user.model.UserModel;
import com.pms.utils.CookieManage;

@Controller
public class DeptController {
	@Autowired
	protected DeptService ds;

	/**
	 * 打开部门首页
	 * @param req 
	 * @param mm
	 * @return
	 */
	@RequestMapping("/deptindex.pms")
	public String deptIndex(HttpServletRequest req,ModelMap mm) {
		CookieManage cm = new CookieManage();
		Map<String, Cookie> map = new HashMap<String, Cookie>();
		map = cm.getCookies(req);
		if (map!=null&&map.size()>0&&map.containsKey("permission")) {
			String permission = map.get("permission").getValue();
			if (permission != null && "yes".equals(permission)) {
				return "dept/deptindex";
			}
		}
		return "redirect:login.pms";
		
	}
	
	/**
	 * 部门树查询
	 * @param request
	 * @param response
	 * @return json格式部门树
	 */
	@RequestMapping(value = "/dept_tree.pms", method = RequestMethod.POST)
	public void quryDepttree(HttpServletRequest req,
			HttpServletResponse resp) {
		
		try {
			JSONArray json = new JSONArray();
			json=ds.queryDeptTree(-1);
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 新增部门节点
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="/dept_insert.pms",method=RequestMethod.POST)
	public void insertDept(HttpServletRequest req,HttpServletResponse resp){
		try {
			String dept_name=req.getParameter("dept_name");
			String dept_spr=req.getParameter("dept_spr");
			String dept_sort=req.getParameter("dept_sort");
			DeptModel dm=new DeptModel();
			dm.setDept_name(dept_name);
			dm.setDept_creater("huyinsong");
			dm.setDept_create_date(new Date(System.currentTimeMillis()));
			dm.setDept_sort(Integer.parseInt(dept_sort));
			if(dept_spr!=null&&!"".equals(dept_spr)){
				dm.setDept_spr(Long.parseLong(dept_spr));
			}else{
				dm.setDept_spr(-1);
			}
			
			JSONObject json = new JSONObject();
			json.put("result", ds.insertDeptService(dm));
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除部门节点
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="dept_delete.pms",method=RequestMethod.POST)
	public void deleteDept(HttpServletRequest req,HttpServletResponse resp){
		try{
			String dept_id=req.getParameter("dept_id");
			if(dept_id!=null&&!"".equals(dept_id)){
				resp.setContentType("application/json; charset=UTF-8");
				ds.deleteDeptService(Long.parseLong(dept_id));
			}
			resp.getWriter().print(0);
			resp.getWriter().close();
		}catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 查询当前所属部门的用户列表
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="dept_user.pms",method=RequestMethod.POST)
	public void queryUserInfo(HttpServletRequest req,HttpServletResponse resp){
		try{
			String dept_id=req.getParameter("dept_id");
			JSONArray userArray=new JSONArray();
			List<UserModel> list=new ArrayList<UserModel>();
			list=ds.getUserList(Long.parseLong(dept_id));
			if(list.size()>0){
				for(UserModel um:list){
					JSONObject userinfo=new JSONObject(um);
					userArray.put(userinfo);
				}
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(userArray);
			resp.getWriter().close();
		}catch(IOException e){
			
		}	
	}
	/**
	 * 添加用户到部门
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="dept_adduser.pms",method=RequestMethod.POST)
	public void addUserToDept(HttpServletRequest req,HttpServletResponse resp){
		String ids[]=req.getParameterValues("user_ids[]");
		String dept_id=req.getParameter("dept_id");
		ds.addUserService(dept_id, ids);
		
	}
	/**
	 * 删除部门下的用户
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="dept_deluser.pms",method=RequestMethod.POST)
	public void delUserToDept(HttpServletRequest req,HttpServletResponse resp){
		String ids[]=req.getParameterValues("user_ids[]");
		String dept_id=req.getParameter("dept_id");
		ds.delUserService(dept_id, ids);
		
	}

}
